package com.deloitte.sapnetweaver.services
{
	import com.deloitte.sapnetweaver.vo.ValueObject;
	
	import flash.utils.Dictionary;
	
	public class ServiceController
	{
		
		private static var serviceController : ServiceController;
		private var instanceMap:Dictionary = new Dictionary();
		//-----------------------------------------------------------
		public static function getInstance():ServiceController
		{
			if ( serviceController == null )
				serviceController = new ServiceController(new Enforcer());
			return serviceController;
		}

		//-----------------------------------------------------------
		public function ServiceController(enforcer:Enforcer):void
		{
			if ( serviceController != null )
				throw new Error( 'Only one ServiceController instance should be instantiated' );
		}
		
		public function execute(serviceClass:Class,valueObject:ValueObject=null):void
		{
			if(serviceClass)
			{
				var key:String =""+serviceClass; 
				var service:IService = instanceMap[key];
				if(service == null)
				{ 
					service = new serviceClass();
					instanceMap[key] = service;
				}
				service.execute(valueObject);
			}
			else
			{
				throw new Error( 'Can not create instance of service,Please check service class' );
			}
		}

	}
}
class Enforcer{}